package com.wangshuo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangshuo.common.JwtHelper;
import com.wangshuo.config.Result;
import com.wangshuo.config.ResultCodeEnum;
import com.wangshuo.entity.Acount;
import com.wangshuo.entity.Admin;
import com.wangshuo.entity.User;
import com.wangshuo.service.UserService;
import com.wangshuo.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
* @author 13326
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-03-05 10:14:33
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Resource
    private JwtHelper jwtHelper;
    @Resource
    private UserMapper userMapper;
    @Override
    public Result login(Acount acount){
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,acount.getUsername());
        User user=this.getOne(queryWrapper);
        if(null==user){
            return Result.build(500, ResultCodeEnum.USERNAME_ERROR);
        }else if(!user.getPassword().equals(acount.getPassword())){
            return Result.build(500,ResultCodeEnum.PASSWORD_ERROR);
        }
        HashMap<String,Object> map=new HashMap<>();
        map.put("token",jwtHelper.createToken(Long.valueOf(user.getId())));
        return Result.ok(map);

    }
    @Override
    public Result register(Acount acount){
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,acount.getUsername());
        User user =this.getOne(queryWrapper);
        if(null!=user){
            return Result.build(500,ResultCodeEnum.USERNAME_USED);
        }else{
            User user1=new User();
            user1.setUsername(acount.getUsername());
            user1.setPassword(acount.getPassword());
            user1.setRole(acount.getRole());
            userMapper.insert(user1);
        }
        return Result.ok("成功");
    }
    @Override
    public IPage<User> getpage(IPage<User> page, String keyword){
        return userMapper.getpage(page,keyword);
    }
}




